#include "TestComboBox.h"
#include<QVBoxLayout>
#include<QComboBox>
#include<QDebug>
#include<QLabel>
#include<QHBoxLayout>
#include<QHash>
#include<QList>
#include<QStringList>
#include<QListView>
#include<QPushButton>

TestComboBox::TestComboBox(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    //1 简单使用
    // 创建一个 QComboBox
    QComboBox *comboBox = new QComboBox();
    layout->addWidget(comboBox);

    // 添加选项 (文本)
    comboBox->addItem("Option 1");
    comboBox->addItem("Option 2");
    comboBox->addItem("Option 3");

    // 添加带图标和用户数据的选项
    comboBox->addItem(QIcon(":/images/logo.png"), "Favorite Option", QVariant(42));

    // 插入一个选项到指定位置
    comboBox->insertItem(1, "Inserted Option");

    // 设置当前选中项 (索引从0开始)
    comboBox->setCurrentIndex(2); // 选中 "Option 3"
    // 连接信号：当选中项改变时
    QObject::connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
                     [](int index) {
        qDebug() << "Current index changed to:" << index;
    });

    // 连接信号：当用户激活（选中）一个项时
    QObject::connect(comboBox, QOverload<int>::of(&QComboBox::activated),
                     [comboBox](int index) {
        QString text = comboBox->itemText(index);
        QVariant data = comboBox->itemData(index);
        qDebug() << "Activated item: Index =" << index
                 << "Text =" << text
                 << "Data =" << data;
    });

    //2 表单输入
    // 创建标签
    QLabel *countryLabel = new QLabel("国家:", this);
    QLabel *provinceLabel = new QLabel("省份:", this);
    QLabel *cityLabel = new QLabel("城市:", this);

    // 创建组合框
    QComboBox *countryCombo = new QComboBox(this);
    QComboBox *provinceCombo = new QComboBox(this);
    QComboBox *cityCombo = new QComboBox(this);

    // 设置占位符文本
    provinceCombo->addItem("-- 请选择省份 --");
    cityCombo->addItem("-- 请选择城市 --");
    QStringList countries;
    countries<<"中国" << "美国" << "日本";
    countryCombo->addItems(countries);
    // 省份数据（按国家分组）
    QMap<QString, QStringList> provinces;
    provinces["中国"] = QStringList() << "北京" << "上海" << "广东" << "江苏" << "浙江";
    provinces["美国"] = QStringList() << "加利福尼亚" << "德克萨斯" << "纽约" << "佛罗里达" << "伊利诺伊";
    provinces["日本"] = QStringList() << "东京都" << "大阪府" << "北海道" << "爱知县" << "神奈川县";

    // 城市数据（按省份分组）
    QMap<QString, QStringList> cities;
    cities["北京"] = QStringList() << "北京市";
    cities["上海"] = QStringList() << "上海市";
    cities["广东"] = QStringList() << "广州市" << "深圳市" << "东莞市" << "佛山市";
    cities["江苏"] = QStringList() << "南京市" << "苏州市" << "无锡市" << "常州市";
    cities["浙江"] = QStringList() << "杭州市" << "宁波市" << "温州市" << "嘉兴市";
    cities["加利福尼亚"] = QStringList() << "洛杉矶" << "旧金山" << "圣何塞" << "圣地亚哥";
    cities["德克萨斯"] = QStringList() << "休斯顿" << "达拉斯" << "奥斯汀" << "圣安东尼奥";
    cities["纽约"] = QStringList() << "纽约市" << "布法罗" << "罗切斯特" << "扬克斯";
    cities["东京都"] = QStringList() << "东京" << "新宿" << "涩谷" << "品川";
    cities["大阪府"] = QStringList() << "大阪" << "堺市" << "高槻市" << "东大阪市";
    cities["北海道"] = QStringList() << "札幌" << "函馆" << "旭川" << "小樽";

    // 国家改变时更新省份
    connect(countryCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, [=](int index){
        if (index < 0) return;

        QString country = countryCombo->currentText();
        provinceCombo->clear();
        cityCombo->clear();

        if (provinces.contains(country)) {
            provinceCombo->addItem("-- 请选择省份 --");
            provinceCombo->addItems(provinces[country]);
        } else {
            provinceCombo->addItem("-- 无可用省份 --");
            cityCombo->addItem("-- 无可用城市 --");
        }
    });

    // 省份改变时更新城市
    connect(provinceCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, [=](int index){
        if (index <= 0) { // 0 是 "-- 请选择省份 --"
            cityCombo->clear();
            cityCombo->addItem("-- 请选择城市 --");
            return;
        }

        QString province = provinceCombo->currentText();
        cityCombo->clear();

        if (cities.contains(province)) {
            cityCombo->addItem("-- 请选择城市 --");
            cityCombo->addItems(cities[province]);
        } else {
            cityCombo->addItem("-- 无可用城市 --");
        }
    });
    QHBoxLayout *hlayout_2 =new QHBoxLayout;
    layout->addLayout(hlayout_2);
    hlayout_2->addWidget(countryLabel);
    hlayout_2->addWidget(countryCombo);
    hlayout_2->addWidget(provinceLabel);
    hlayout_2->addWidget(provinceCombo);
    hlayout_2->addWidget(cityLabel);
    hlayout_2->addWidget(cityCombo);

    //3 使用自定义模型和视图
    QComboBox *comboBox3 =new QComboBox(this);
    CustomComboModel *model = new CustomComboModel;
    model->appendRow(new QStandardItem("Important Item"));
    model->appendRow(new QStandardItem("Normal Item"));
    comboBox3->setModel(model);
    // 创建自定义视图（带复选框）
    QListView *listView = new QListView;
    listView->setSelectionMode(QAbstractItemView::MultiSelection);
    comboBox3->setView(listView);
    // 多选处理
    connect(comboBox3, QOverload<int>::of(&QComboBox::activated), [=](int index){
        QModelIndex modelIndex = comboBox3->model()->index(index, 0);
        Qt::CheckState state = static_cast<Qt::CheckState>(modelIndex.data(Qt::CheckStateRole).toInt());
        comboBox3->model()->setData(modelIndex,
                                state == Qt::Checked ? Qt::Unchecked : Qt::Checked,
                                Qt::CheckStateRole);
    });
    layout->addWidget(comboBox3);

    //4 完全自定义弹出窗口
    CustomComboBox2 *Custom_ComboBox2 =new CustomComboBox2(this);
    layout->addWidget(Custom_ComboBox2);

}
CustomComboPopup::CustomComboPopup(QWidget *parent)
    :QWidget(parent)
{
    // 关键设置：必须设置窗口标志为 Popup
    setWindowFlags(Qt::Popup);

    // 设置模态行为（可选，根据需求）
    setAttribute(Qt::WA_ShowWithoutActivating);

    QVBoxLayout *layout = new QVBoxLayout(this);

    QPushButton *btn1 = new QPushButton("Custom Button 1", this);
    QPushButton *btn2 = new QPushButton("Custom Button 2", this);
    QPushButton *btn3 = new QPushButton("Custom Button 3", this);

    layout->addWidget(btn1);
    layout->addWidget(btn2);
    layout->addWidget(btn3);

    connect(btn1, &QPushButton::clicked,this, [this]() {
        emit selected("Custom 1");
        hide();
    });
    connect(btn2, &QPushButton::clicked, [this]() { emit selected("Custom 2"); hide(); });
    connect(btn3, &QPushButton::clicked, [this]() { emit selected("Custom 3"); hide(); });

    setLayout(layout);

    // 设置合适的最小大小
    setMinimumSize(120, 100);
}
